<?php
	class fileUpload
	{
		var $uploadDirectory;
		var $allowedSize = 50000000;					
		var $extensionCheck = TRUE;
		
		var $allowedExtensions = array 
		(
			'zip'	=> TRUE,
			'7z'	=> TRUE,
			'rar'	=> TRUE,	
			'txt'	=> TRUE,
			'pdf'	=> TRUE,
			'doc' 	=> TRUE,
			'docx' 	=> TRUE,
			'xls'	=> TRUE,
			'ppt'	=> TRUE,	  
			'exe'	=> FALSE,	
			'gif'	=> TRUE,
			'png'	=> TRUE,
			'jpg'	=> TRUE,
			'jpeg'	=> TRUE,
			'mp3'	=> TRUE,
			'mp4'	=> TRUE,
		);
		
		var $fid,$uid,$name,$fullName,$tmpName,$newName,$extension,$size;
		
		public function __construct()
		{
			$this->uploadDirectory = UPLOADPATH;
		}
		
		public function doUpload($file,$owner,$uid)
		{
			$this->name = substr(md5(time()), 0, 5).'.'.substr($file['name'], 0, strripos($file['name'], '.'));
			$this->fullName = $file['name'];
			$this->extension = strtolower(substr($file['name'], strripos($file['name'], '.') + 1));
			$this->tmpName = $file['tmp_name'];
			$this->newName = $this->name.'.'.$this->extension;
			$this->size = $file['size']; 
			$this->uid = $uid;
			if ($this->fileCheck())
			{
				$this->getFid();
				if ($this->moveFile() == 0)
				{
					$this->newName = iconv("gb2312","UTF-8",$this->newName);
					$this->adaptInfo();
					return 0;
				}
				else 
				{
					return 2;
				}
			}
			else
			{
				return 1;
			}
		}
		
		private function moveFile($overWrite = FALSE)
		{
			if ($overWrite)
			{
				move_uploaded_file($this->tmpName, $this->uploadDirectory.$this->newName);
				return 0;
			}
			else
			{
				if (file_exists($this->uploadDirectory.$this->newName))
				{
					return 1;
				}
				else 
				{
					$this->newName = iconv("UTF-8","gb2312",$this->newName);
					if (move_uploaded_file($this->tmpName, $this->uploadDirectory.$this->newName))
					{
						return 0;
					}
				}
			}
			
		}
		
		private function getFid()
		{
			$conn = new mysqlClass();
			$query = "SELECT * FROM record WHERE name='fileAmount';";
			$conn->query($query);
			$conn->fetch();
				
			$this->fid = $conn->data['value'] + 1;
		}
		
		private function adaptInfo()
		{
			$conn = new mysqlClass();
			$query = "INSERT INTO files (fid,fileName,fileOriname,fileStatus,fileLevel,fileOwner) VALUES ({$this->fid},'{$this->newName}','{$this->fullName}','Shared','Normal',{$this->uid});";
			$conn->query($query);
			$query = "UPDATE record SET value={$this->fid} WHERE name='fileAmount';";
			$conn->query($query);
		}
		
		private function fileCheck()
		{
	    	$flag = TRUE;
	    	if ($this->extensionCheck == TRUE)
	    	{
	    		if (array_key_exists($this->extension, $this->allowedExtensions) && $this->allowedExtensions[$this->extension] == TRUE)
	    		{
	    		}
	    		else 
	    		{
	    			$flag = FALSE;
	    		}
	    	}
	    	if ($this->size > $this->allowedSize)
	    	{
	    		$flag = FALSE;
	    	}
	    	return $flag;
		}
	}
	
	class thisFile
	{
		var $fid,$name,$oriname,$pureName,$extension,$owner,$status;
		var $uploadDirectory,$unsharedDirectory,$trashDirectory;
		
		public function __construct($data = array(),$upload = UPLOADPATH, $unshared = UNSHAREDPATH, $trash = TRASHPATH)
		{
			$this->fid = $data['fid'];
			$this->name = $data['fileName'];
			$this->oriname = $data['fileOriname'];
			$this->pureName = substr($this->oriname, 0, strripos($this->oriname, '.'));
			$this->extension = strtolower(substr($this->oriname, strripos($this->oriname, '.') + 1));
			$this->owner = $data['fileOwner'];
			$this->status = $data['fileStatus'];
			$this->uploadDirectory = $upload;
			$this->trashDirectory = $trash;
			$this->unsharedDirectory = $unshared;
		}
		
		public static function getFileByFid($fid)
		{
			$conn = new mysqlClass();
			$query = "SELECT * FROM files WHERE fid={$fid};";
			$conn->query($query);
			if ($conn->row == 0)
			{
				return NULL;
			}
			else 
			{
				$conn->fetch();
				return new thisFile($conn->data);
			}
		}
		
		public function update()
		{
			$conn = new mysqlClass();
			$oriname = $this->pureName.'.'.$this->extension;
			$full = substr(md5(time()),0,5).'.'.$oriname;
			$query = "UPDATE files SET fileName='{$full}', fileOriname='{$oriname}', fileStatus='{$this->status}', fileOwner='{$this->owner}' WHERE fid={$this->fid};";
			$full = iconv("UTF-8","gb2312",$full);
			$this->name = iconv("UTF-8","gb2312",$this->name);
			if ($conn->query($query))
			{
				if ($this->status == "Shared")
				{
					if (file_exists($this->unsharedDirectory.$this->name))
					{
						rename($this->unsharedDirectory.$this->name, $this->uploadDirectory.$this->name);
					}
					rename($this->uploadDirectory.$this->name, $this->uploadDirectory.$full);
				}
				else
				{
					if (file_exists($this->uploadDirectory.$this->name))
					{
						rename($this->uploadDirectory.$this->name, $this->unsharedDirectory.$this->name);
					}
					rename($this->unsharedDirectory.$this->name, $this->unsharedDirectory.$full);
				}
				return TRUE;
			}
			else
			{
				return FALSE;
			}		
		}
		
		public function del()
		{
			$conn = new mysqlClass();
			$query = "DELETE FROM files WHERE fid={$this->fid};";
			$this->name = iconv("UTF-8","gb2312",$this->name);
			if (file_exists($this->uploadDirectory.$this->name))
			{
				rename($this->uploadDirectory.$this->name, $this->trashDirectory.$this->name);
			}
			if (file_exists($this->unsharedDirectory.$this->name))
			{
				rename($this->unsharedDirectory.$this->name, $this->trashDirectory.$this->name);
			}
			return $conn->query($query);
		}
	}
	
	class imageFile
	{
		var $uploadDirectory;
		var $allowedSize = 512000000;					
		var $extensionCheck = TRUE;
		
		var $allowedExtensions = array 
		(
			'gif'	=> TRUE,
			'png'	=> TRUE,
			'jpg'	=> TRUE,
			'jpeg'	=> TRUE,
		);
		
		var $iid,$owner,$name,$tmpName,$newName,$extension,$size;
		
		public function __construct()
		{
			$this->uploadDirectory = UPLOADPATH;
		}
		
		public function doUpload($file,$owner)
		{
			$this->name = substr(md5(time()), 0, 5).'.'.substr($file['name'], 0, strripos($file['name'], '.'));
			$this->extension = strtolower(substr($file['name'], strripos($file['name'], '.') + 1));
			$this->tmpName = $file['tmp_name'];
			$this->newName = $this->name.'.'.$this->extension;
			$this->size = $file['size']; 
			$this->owner = $owner;
			
			if ($this->fileCheck())
			{
				$this->getIid();
				if ($this->moveFile() == 0)
				{
					$this->newName = iconv("gb2312","UTF-8",$this->newName);
					$this->adaptInfo();
					return 0;
				}
				else 
				{
					return 2;
				}
			}
			else
			{
				return 1;
			}
		}
		
		public static function getImageByAid($aid)
		{
			$conn = new mysqlClass();
			$query = "SELECT * FROM images WHERE imageOwner='{$aid}';";
			return $conn->query($query);
		}
		
		public static function getImageByIid($iid)
		{
			$conn = new mysqlClass();
			$query = "SELECT * FROM images WHERE iid={$iid};";
			$conn->query($query);
			$conn->fetch();
			return $conn->data;
		}
		
		public static function getAlbumByAid($aid)
		{
			$conn = new mysqlClass();
			$query = "SELECT * FROM albums WHERE aid={$aid};";
			$conn->query($query);
			$conn->fetch();
			return $conn->data;
		}

		public static function delAlbumByAid($aid)
		{
			$conn = new mysqlClass();
			$query = "DELETE FROM albums WHERE aid={$aid};";
			if ($conn->query($query))
			{
				$query = "DELETE FROM images WHERE imageOwner='{$aid}';";
				return $conn->query($query);
			}
			else
			{
				return FALSE;
			}
		}
		
		public static function updateAlbumName($aid,$albumName)
		{
			$conn = new mysqlClass();
			$query = "UPDATE albums SET albumName='{$albumName}' WHERE aid={$aid};";
			return $conn->query($query);
		}
		
		private function moveFile($overWrite = FALSE)
		{
			if ($overWrite)
			{
				move_uploaded_file($this->tmpName, $this->uploadDirectory.$this->newName);
				return 0;
			}
			else
			{
				if (file_exists($this->uploadDirectory.$this->newName))
				{
					return 1;
				}
				else 
				{
					$this->newName = iconv("UTF-8","gb2312",$this->newName);
					move_uploaded_file($this->tmpName, $this->uploadDirectory.$this->newName);
					return 0;
				}
			}
			
		}
		
		private function getIid()
		{
			$conn = new mysqlClass();
			$query = "SELECT * FROM record WHERE name='imagesAmount';";
			$conn->query($query);
			$conn->fetch();
				
			$this->iid = $conn->data['value'] + 1;
		}
		
		private function adaptInfo()
		{
			$conn = new mysqlClass();
			$query = "INSERT INTO images (iid,imageName,imageStatus,imageOwner) VALUES ({$this->iid},'{$this->newName}','Shared','{$this->owner}');";
			$conn->query($query);
			$query = "UPDATE record SET value={$this->iid} WHERE name='imagesAmount';";
			$conn->query($query);
		}
		
		private function fileCheck()
		{
	    	$flag = TRUE;
	    	if ($this->extensionCheck == TRUE)
	    	{
	    		if (array_key_exists($this->extension, $this->allowedExtensions) && $this->allowedExtensions[$this->extension] == TRUE)
	    		{
	    		}
	    		else 
	    		{
	    			$flag = FALSE;
	    		}
	    	}
	    	if ($this->size > $this->allowedSize)
	    	{
	    		$flag = FALSE;
	    	}
	    	return $flag;
		}
	}